package leetcode.offer0055;

import java.util.ArrayList;
import java.util.List;

import leetcode.helper.tree.TreeNode;

//执行用时：16 ms, 在所有 Java 提交中击败了99.70%的用户
//
//内存消耗：44.9 MB, 在所有 Java 提交中击败了8.93%的用户
//
//通过测试用例：61 / 61
public class BSTIterator {

	Integer[] node;
	int p;
	int size;

	public BSTIterator(TreeNode root) {
		List<Integer> nodes = new ArrayList<>();
		this.f(root, nodes);
		node = nodes.toArray(new Integer[0]);
		this.p = 0;
		this.size = node.length;
	}

	private void f(TreeNode root, List<Integer> nodes) {
		if (root == null) {
			return;
		}
		this.f(root.left, nodes);
		nodes.add(root.val);
		this.f(root.right, nodes);
	}

	public int next() {
		return this.node[p++];
	}

	public boolean hasNext() {
		return p < size;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
